//
//  DENMyViewController.m
//  Icar
//
//  Created by apple on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "DENMyViewController.h"
#import "LYYMineTableViewCell.h"
#import "LYYBreakRuleViewController.h"
#import "DXAlertView.h"
#import "LYYRadioViewController.h"
@interface DENMyViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSArray *titleArray;
@property (nonatomic,retain)NSArray *imageArray;
@property (nonatomic,retain)UIButton *button;

@end

@implementation DENMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH(375), HEIGHT(64))];
    [titleView setBackgroundColor:BLUECOLOR];
    [self.view addSubview:titleView];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setFrame:CGRectMake(self.view.bounds.size.width - WIDTH(60), 15, WIDTH(50), HEIGHT(50))];
    if ([LYYNightModeHandle shareNightModeHandle].temp == NO) {
        [self.button setImage:[UIImage imageNamed:@"iconfont-yejianzhuanhuan"] forState:UIControlStateNormal];
    }
    if ([LYYNightModeHandle shareNightModeHandle].temp == YES) {
        [self.button setImage:[UIImage imageNamed:@"iconfont-qingbaitian"] forState:UIControlStateSelected];
    }
    [self.button addTarget:self action:@selector(changeNightMode:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:self.button];
    
    self.tableViewo = [[UITableView alloc]initWithFrame:CGRectMake(0, HEIGHT(214) , WIDTH(375), HEIGHT(453)) style:UITableViewStylePlain];
    self.tableViewo.delegate = self;
    self.tableViewo.dataSource = self;
    [self.view addSubview:self.tableViewo];
    [self.tableViewo setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.titleArray = @[@"违章查询",@"清除缓存",@"汽车电台"];
    self.imageArray = @[@"1121",@"iconfont-lajitong",@"iconfont-yinlediantai"];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, HEIGHT(64), WIDTH(375), HEIGHT(150))];
    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];
    [self.view addSubview:imageView];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self.tableViewo reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cee = @"cell";
    LYYMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cee];
    if (!cell) {
        cell = [[LYYMineTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cee];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.imgView setImage:[UIImage imageNamed:self.imageArray[indexPath.row]]];
    [cell.label setText:self.titleArray[indexPath.row]];
    return cell;
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        LYYBreakRuleViewController *breakRuleVC = [[LYYBreakRuleViewController alloc]init];
        [self.navigationController pushViewController:breakRuleVC animated:YES];
    }else if (indexPath.row == 1){
        
#pragma mark - 点击清除缓存
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
        NSString *cache = [NSString stringWithFormat:@"%.1fM",[self folderSizeAtPath:path]];
        
#pragma mark- DXAlertView
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"提示" contentText:[ NSString stringWithFormat:@"确定清除全部缓存?\n(%@)",cache] leftButtonTitle:@"取消" rightButtonTitle:@"确认"];
        [alert show];
        alert.leftBlock = ^() {
            NSLog(@"left");
        };
        alert.rightBlock = ^() {
            NSLog(@"right");
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
            NSString *path = [paths lastObject];
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
            for (NSString *p in files) {
                NSError *error;
                NSString *Path = [path stringByAppendingPathComponent:p];
                if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
                    [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
                }
            }
            
        };
        alert.dismissBlock = ^() {
            NSLog(@"dismiss");
        };
    }else if (indexPath.row == 2){
        LYYRadioViewController *radioVC = [[LYYRadioViewController alloc]init];
        [self.navigationController pushViewController:radioVC animated:YES];
        
    }
    
}

#pragma mark- 计算缓存
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

#pragma mark - 夜间模式
- (void)changeNightMode:(UIButton *)sender{
  
    if ([LYYNightModeHandle shareNightModeHandle].temp == NO) {
        [self.view setBackgroundColor:[UIColor blackColor]];
        [LYYNightModeHandle shareNightModeHandle].nightModeColor = [UIColor blackColor];
        [LYYNightModeHandle shareNightModeHandle].backColor = [UIColor colorWithWhite:0.200 alpha:1.000];
        [LYYNightModeHandle shareNightModeHandle].wordColor = [UIColor whiteColor];
        [self.button setImage:[UIImage imageNamed:@"iconfont-qingbaitian"] forState:UIControlStateSelected];
        [self.tableViewo setBackgroundColor:[UIColor blackColor]];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"night" object:nil];
    }
    if ([LYYNightModeHandle shareNightModeHandle].temp == YES) {
        [self.view setBackgroundColor:[UIColor whiteColor]];
        [LYYNightModeHandle shareNightModeHandle].nightModeColor = [UIColor whiteColor];
        [LYYNightModeHandle shareNightModeHandle].backColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        [LYYNightModeHandle shareNightModeHandle].wordColor = [UIColor blackColor];
        [LYYNightModeHandle shareNightModeHandle].temp = YES;
        [self.button setImage:[UIImage imageNamed:@"iconfont-yejianzhuanhuan"] forState:UIControlStateNormal];
        [self.tableViewo setBackgroundColor:[UIColor whiteColor]];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"night" object:nil];
    }
    [LYYNightModeHandle shareNightModeHandle].temp = ![LYYNightModeHandle shareNightModeHandle].temp;
    sender.selected = !sender.selected;
    [self.tableViewo reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
